<!--  -->
<template>
    <div>
      <reception-header/>
      <div class="warn-style">
        <el-row>
          <el-col :span="1">
            <div class="icon-style"><i class="el-icon-warning"></i></div>
          </el-col>
          <el-col :span="23">
            <span class="tip-conten-style">
              提示每一位求职者：若用人单位存在提供虚假招聘信息、发布虚假招聘广告，以担保或者其他任何名义向求职者收取财物（如办卡费、押金、培训费），扣押或以保管为名索要身份证、毕业证及其他证件等行为，均涉嫌违法，请您提高警惕并注意保护个人信息。
            </span>
          </el-col>
        </el-row>
      </div>
      <div class="position-header-style">
        <div>
          <i class="el-icon-timer"></i><span class="publishTime-style">发布时间 {{publishTime}}</span>
        </div>
        <div>
          <span class="positionName-style">{{positionName}}</span>
          <el-tag effect="dark" type="success" size="mini" class="xiao-style">校</el-tag>
        </div>
        <div class="companyName-style" @click="returnPreviousPage">
          <span>{{companyName}}</span>
        </div>
        <div>
          <el-row>
            <el-col :span="16">
              <div>
                <div>
                  <span class="salary-style">{{salary}}</span>
                  <span class="hangye-style"><i class="el-icon-office-building"></i>&nbsp;行业薪资对比</span>
                </div>
                <div class="muilt-info">
                  <span>{{workAddress}} | </span>
                  <span>{{jobType}} | </span>
                  <span>{{education}} | </span>
                  <span>{{recruitPeopleCount}}</span>
                </div>
              </div>
              <div class="state-style" v-show="invalid">
              </div>
            </el-col>
            <el-col :span="8" class="btn-style">
              <el-button type="success" plain v-if="!isCollection" @click="collectionFunc"><i class="el-icon-star-off"></i>收藏</el-button>
              <el-button type="success" plain v-if="isCollection" @click="CancelCollectionFunc"><i class="el-icon-star-on"></i>取消收藏</el-button>
              <el-button type="primary" @click="deliverHandle" :disabled="disabled">{{deliver}}</el-button>
            </el-col>
          </el-row>
        </div>
      </div>
      <div class="main-content-style">
        <el-row>
          <el-col :span="18">
            <el-divider>
              <span class="introduction">职位描述</span>
            </el-divider>
            <div class="positionDecription-style">
              <span style="white-space: pre-wrap;">{{positionDecription}}</span>
            </div>
            <div>
               <el-button type="primary" @click="deliverHandle" :disabled="disabled" class="toudi-btn">{{deliver}}</el-button>
            </div>
          </el-col>
          <el-col :span="6">
            <div class="right-company">
              <div><img :src="companyAvatarUrl" alt="" class="companyImage-style"></div>
              <div class="company-name-style"><span>{{companyName}}</span></div>
              <div class="company-people-count"><i class="el-icon-user-solid"></i> {{companyPeopleCount}}</div>
              <div class="company-people-count"><i class="el-icon-location-outline"></i> {{companyAddress}}</div>
              <div class="introduction">&emsp;&emsp;{{companyIntroduction}}</div>
            </div>
            <div class="toCompany-main-page">
              <el-button type="primary" plain @click="returnPreviousPage">公司主页</el-button>
              <el-button v-if="remarks === '用户'" type="primary" plain @click="toMessagePage">打招呼</el-button>
            </div>
          </el-col>
        </el-row>
      </div>
    </div>
</template>

<script>
import ReceptionHeader from "../../../components/ReceptionHeader.vue";
import { getPositionAllInfo } from "../../../api/recruitmentInfoManage";
import { getCurrentLoginRoleName } from "../../../api/dataViewApi/index";
import { getStore } from "../../../storage/storage";
import { userCollection,checkThisWorkIsCollection,cancelCollection } from "../../../api/collection/index";
import { checkHaveResume,deliverResume,checkIsDeliver } from "../../../api/personalApi";
import { insertMessage } from "../../../api/message/index";
export default {
  components: { ReceptionHeader },
  data () {
    return {
      companyRecruitAid: '', // 公司-招聘aid
      recruitmentInfoAid: '', // 招聘信息aid
      companyAid: '',         // 公司aid
      publishTime: '2021-08-27', // 发布时间
      positionName: '前端开发工程师', // 岗位名字
      companyName: '中国联合网络通信集团有限公司-云南联通新通信有限公司', // 公司名称
      salary: '面议', // 工资
      workAddress: '昆明-官渡区', // 工作地址
      jobType: '全职', // 工作类型
      education: '本科及以上', // 学历要求
      recruitPeopleCount: '2人', // 招聘人数
      isCollection: false, // 是否收藏
      positionDecription: '', // 职位描述
      companyAvatarUrl: 'https://recruitment-files.oss-cn-chengdu.aliyuncs.com/user/youshang1.jpg',  // 公司头像url
      companyPeopleCount: '1000人', // 公司人数
      companyAddress: '北京市海定区', // 公司地址
      companyIntroduction: '小米公司2010年4月成立，是一家专注于高端智能手机自主研发的移动互联网公司，由前Google、微软、金山等公司的顶尖高手组建。 [35] 目前已获得来自Morningside、启明、IDG和小米团队4100万美元投资，其中小米团队56人投资1100万美元，公司估值2.5亿美元。2010年底推出手机实名社区米聊，在推出半年内注册用户突破300万。此外，小米公司还推出手机操作系统MIUI，当年6月底MIUI社区活跃用户达30万。2011年8月16日，小米公司通过媒体沟通会正式发布小米手机、米聊、MIUI是小米科技的三大核心产品。',
      companyAccount: '',
      remarks: '', // “管理员” 或者 “用户” 或者 “企业”
      deliver: '立即投递',
      invalid: false, // 职位是否失效
      disabled: false, // 按钮禁用状态
    }
  },
  created(){
    this.recruitmentInfoAid = this.$route.query.recruitmentInfoAid;
    this.companyAid = this.$route.query.companyAid;
    
    // 获取招聘详情的所有信息
    this.getPositionAllInfoFunc();

    // 判断当前这个职位是否被收藏
    this.checkThisWorkIsCollectionFunc();

  },
  methods:{

    // 去公司页
    returnPreviousPage(){
       this.$router.push({
        path: '/reception/companyAllRecruitmentInfo',
        query: {
          name: this.companyAccount,
        }
      });
    },

    // 判断当前这个职位是否被收藏
    checkThisWorkIsCollectionFunc(){
      const params = {
          account: getStore("account"),
      }
      const _this = this;

      setTimeout(() => {
        getCurrentLoginRoleName(params).then(res=>{
          this.remarks = res.data.data.remarks;
          if(res.data.data.remarks === '用户' || res.data.data.remarks === '管理员'){
            const params = {
              companyRecruitAid: _this.companyRecruitAid,
              account: getStore("account"),
            }
            checkThisWorkIsCollection(params).then(res=>{
              // console.log(res);
              if (res.data.data) {
                this.isCollection = true;
              }
            })
          }
        });
      }, 100);
    },

    // 收藏
    collectionFunc(){
        const params = {
          account: getStore("account"),
        }
        const _this = this;
        getCurrentLoginRoleName(params).then(res=>{
            if(res.data.data.remarks === '用户' || res.data.data.remarks === '管理员'){
              this.remarks = res.data.data.remarks;
              const params = {
                companyRecruitAid: _this.companyRecruitAid,
                account: getStore("account"),
              }
              userCollection(params).then(res=>{
                _this.isCollection = !_this.isCollection;
                _this.$message({
                  type: 'success',
                  message: res.data.message,
                  duration: 500,
                });
              })             
            }else{
              _this.$message({
                message: "公司账号不能收藏！",
                type: 'error',
                duration: 600
              });
            }
        })
    },

    // 取消收藏
    CancelCollectionFunc(){
      const _this = this;
      this.$confirm('确定取消收藏该岗位吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const params = {
          companyRecruitAid: _this.companyRecruitAid,
          account: getStore("account"),
        }
        cancelCollection(params).then(res=>{
          _this.$message({
            type: 'success',
            message: res.data.message,
            duration: 500,
          });
          _this.isCollection = !_this.isCollection;
        })
      })
    },

    // 立即投递
    deliverHandle(){
      if(this.deliver === '已投递'){
        this.$message({
          message: '您已经投递！',
          type: 'info',
          duration: 500,
        })
      }else{
        const params = {
          account: getStore("account"),
        }
        const _this = this;
        // 判断当前登陆的是什么角色（如果是用户，就可以投递；如果是管理员和企业，就提示不能投递）
        getCurrentLoginRoleName(params).then(res=>{
          if(res.data.data.remarks === '企业' || res.data.data.remarks === '管理员'){
            _this.$message({
              message: '您不是求职者，不能投递简历！',
              type: 'error',
              duration: 900,
            })
          }else if(res.data.data.remarks === '用户'){
            // 判断是否有简历
            const params = {
              account: getStore("account"),
            }
            checkHaveResume(params).then(res=>{
              if(res.data.data){ // 有简历
                // 投递简历
                const params = {
                  account: getStore("account"), // 当前登录人账号
                  companyRecruitAid: _this.companyRecruitAid,
                }
                deliverResume(params).then(res=>{
                  _this.$message({
                    message: res.data.message,
                    type: 'success',
                    duration: 800,
                  })
                  _this.deliver = '已投递';
                })
              }else{ // 无简历，去完善简历
                _this.$message({
                  message: '您还没有简历，先去完善简历吧！',
                  type: 'info',
                  duration: 800,
                });
                setTimeout(() => {
                  _this.$router.push("/reception/editMyResume");
                }, 800);
              }
            })
          }
        });
      } 
    },

    // 获取招聘详情的所有信息
    getPositionAllInfoFunc(){
      const param = {
        companyAid: this.companyAid,
        recruitmentInfoAid: this.recruitmentInfoAid,
      }
      getPositionAllInfo(param).then(res=>{
        // console.log(res);
        this.companyRecruitAid = res.data.data.companyRecruitAid;
        this.companyAccount = res.data.data.companyAccount;
        this.publishTime = res.data.data.publishTime;
        this.positionName = res.data.data.positionName;
        this.companyName = res.data.data.companyName;
        this.salary = res.data.data.salary;
        this.workAddress = res.data.data.workAddress;
        this.jobType = res.data.data.jobType;
        this.recruitPeopleCount = (res.data.data.recruitPeopleCount === 'null人') ? '10人' : 'res.data.data.recruitPeopleCount';
        this.education = res.data.data.education ? res.data.data.education : '学历不限';
        this.positionDecription = res.data.data.positionDecription;
        this.companyAvatarUrl = res.data.data.companyAvatarUrl ? res.data.data.companyAvatarUrl : 'https://recruitment-files.oss-cn-chengdu.aliyuncs.com/user/hr1.jpg';
        this.companyPeopleCount = (res.data.data.companyPeopleCount === 'null人') ? '员工人数不详' : res.data.data.companyPeopleCount;
        this.companyAddress = res.data.data.companyAddress;
        this.companyIntroduction = res.data.data.companyIntroduction ? res.data.data.companyIntroduction : '公司介绍暂无...';
        // 判断当前这个职位是否已经投递
        const params = {
          account: getStore("account"),
          companyRecruitAid: this.companyRecruitAid,
        }
        const _this = this;
        checkIsDeliver(params).then(res=>{
          if (res.data.data) {
            _this.deliver = '已投递';
          }else{
            _this.deliver = '立即投递';
          }
        })
        this.invalid = res.data.data.invalid;
        if (this.invalid) {
          this.disabled = true;
        }else{
          this.disabled = false;
        }
      })
    },

    // 打招呼（去消息界面）
    toMessagePage(){
      // 插入一条消息（给公司打招呼）
      const params = {
        account: getStore("account"),
        companyAid: this.companyAid,
      }
      insertMessage(params).then((res)=>{
        // console.log(res);
        if (res.data.code === 10001) {
          this.$message.info(res.data.message);
        }else if(res.data.code === 10000){
          this.$router.push({
            path: '/reception/myMessagePage',
          })
        }
      });

    }

  }
}
</script>

<style scoped>
@import "../../../assets/css/reception/recruitDetail.css";
</style>
